<template>
	<view class="content">
		<view class="" v-if="datalist.length > 0">
			<view class="account_item" style="display: flex;justify-content: space-between;align-items: center;" v-for="(item,index) in datalist">
				<view class="" style="display: flex;flex-direction: column;">
					<view class="" style="display: flex;justify-content: space-between;align-items: center;width: 100%;">
						{{item.legal_name}}({{item.mobile}})
						
					</view>
					<view class="" style="color: #7e7e7e;margin-top: 10rpx;font-size: 24rpx;">
						{{item.province_text}}-{{item.city_text}}-{{item.area_text}}
					</view>
					<view class="" style="color: #7e7e7e;margin-top: 10rpx;font-size: 24rpx;">
						账号状态：<span v-if="item.state == 0" style="color: red;">待审核</span><span v-if="item.state == 1" style="color: green;">正常</span><span v-if="item.state == 0" style="color: red;">已驳回</span>
					</view>
				</view>
				<view class="">
					<view class="btns" v-if="userinfo.id != item.id" @click="qiehuan(item.id)" >
						切换账号
					</view>
					<view class="btns" v-else style="color: #555555;background-color: #ccc;">
						当前登录
					</view>
				</view>
			</view>
		</view>
		<view class="" v-else style="width: 100%;display: flex;justify-content: center;flex-direction: column;align-items: center;margin-top: 300rpx;">
			<image :src="url+'wechatimg/emptydata.png'" mode="" style="width: 300rpx;height: 200rpx;margin: 0px auto;"></image>
			<view class="" style="color: #7e7e7e;">
				暂无数据
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				url: this.global.url,
				datalist:[],
				userinfo:{}
			}
		},
		onLoad() {
			this.getuserinfo()
			this.getdata()
		},
		methods: {
			// 切换为这个账号登录
			qiehuan(id){
				var that = this
				uni.showLoading({
					title:'切换中...'
				})
				uni.request({
				    url: that.global.url + 'api/user/switch_accounts',
				    method: "POST",
					header: {
						'token': uni.getStorageSync('token')
					},
				    data: {
						id:id
					},
				    success(res) {
				        let result = res.data
						console.log(result)
				        if(result.code == 1){
							// 存用户信息
							uni.setStorageSync('userinfo', result.data.userinfo)
							// 存用户信息
							uni.setStorageSync('token', result.data.userinfo.token)
							uni.showToast({
								title: result.msg,
								icon: 'success'
							})
							setTimeout(()=>{
								uni.navigateBack()
							},1000)
						}else{
							uni.removeStorageSync('userinfo')
							uni.removeStorageSync('token')
							// uni.setStorageSync('userinfo', result.data.userinfo)
							// // 存用户信息
							// uni.setStorageSync('token', result.data.userinfo.token)
				           uni.showToast({
				           	title: result.msg,
				           	icon: 'error'
				           })
						   setTimeout(()=>{
							   uni.navigateBack()
						   },1000)
				        }
				    },complete() {
				    	uni.hideLoading()
				    }
				})
			},
			getuserinfo(){
				var that = this
				uni.request({
				    url: that.global.url + 'api/user/index',
				    method: "POST",
					header: {
						'token': uni.getStorageSync('token')
					},
				    data: {},
				    success(res) {
				        let result = res.data
						console.log(result)
				        if(result.code == 1){
							that.userinfo = result.data
						}else{
							uni.removeStorageSync('userinfo')
							uni.removeStorageSync('token')
							// uni.setStorageSync('userinfo', result.data.userinfo)
							// // 存用户信息
							// uni.setStorageSync('token', result.data.userinfo.token)
				           uni.showToast({
				           	title: result.msg,
				           	icon: 'error'
				           })
						   setTimeout(()=>{
							   uni.navigateBack()
						   },1000)
				        }
				       
				    }
				})
			},
			getdata(){
				var that = this
				uni.showLoading({
					title: '加载中...'
				})
				uni.request({
					url: this.global.url + 'api/user/findmyaccount',
					method: 'POST',
					header: {
						'token': uni.getStorageSync('token')
					},
					data: {
						// clinic_id: uni.getStorageSync('token')
					},
					success(res) {
						let result = res.data
						console.log(result)
						if (result.code == 1) {
							// 存用户信息
							that.datalist = result.data
				
						} else {
							uni.showToast({
								title: result.msg,
								icon: 'none'
							})
						}
					},
					complete() {
						uni.hideLoading()
					}
				})
			}
		}
	}
</script>

<style>
.content{
	padding: 20rpx;
	margin: 0px auto;
	width:90%;
	margin-top: 20px;
}
.account_item{
	padding: 20rpx;
	border-radius: 15rpx;
	width: 94%;
	border: 1px solid #ccc;
	margin-bottom: 15rpx;
}
.btns{
	padding: 10rpx 15rpx;
	color: #fff;
	background-color: #1262DB;
	border-radius: 10px;
}
</style>
